package concurrent;

import lombok.extern.slf4j.Slf4j;

/**
 * @author fengpeng
 * @date 2019-08-31  16:52
 */
@Slf4j
public class DeadLockDemo {

    private static Object lock1 = new Object();

    private static Object lock2 = new Object();

    private static   class Thread1 extends Thread{

        @Override
        public void run() {
           synchronized (lock1){
               try {
                   Thread.sleep(5000L);
                   log.info("thread1 get lock1....");
               } catch (InterruptedException e) {
                   log.error("thread1",e);
               }
               synchronized (lock2){
                   log.info("thread1 get lock2....");
               }
           }
        }
    }

    private static   class Thread2 extends Thread{

        @Override
        public void run() {
            synchronized (lock2){
                try {
                    Thread.sleep(5000L);
                    log.info("thread2 get lock2....");
                } catch (InterruptedException e) {
                    log.error("thread1",e);
                }
                synchronized (lock1){
                    log.info("thread2 get lock1....");
                }
            }
        }
    }

    public static void main(String[] args){
        Thread1 thread1 = new Thread1();
        thread1.start();
        Thread2 thread2 = new Thread2();
        thread2.start();
    }
}
